﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DTC.Database;

namespace DTC.Popups
{
    /// <summary>
    /// Interaction logic for ManufacturerPopup.xaml
    /// </summary>
    public partial class ManufacturerPopup : Window
    {
        public ManufacturerPopup()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            manufacturer = new DTCManufacturer();
            MainGrid.DataContext = manufacturer;
        }

        private DTCManufacturer manufacturer;
        public DTCManufacturer Manufacturer
        {
            get
            {
                return manufacturer;
            }
            set
            {
                if (manufacturer != value)
                {
                    manufacturer = value;
                    MainGrid.DataContext = manufacturer;
                }
            }
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.DialogResult == true)
            {
                ManufacturerTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                CountryTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                List<string> errList = new List<string>();
                string message = "";
                if (new DTC.Validations.FormValidate().FormHasError(errList, MainGrid))
                {
                    foreach (string str in errList)
                        //if(!(message.Contains(str)))
                        message += str;
                    WarningPopup wp = new WarningPopup();
                    wp.Owner = this;
                    wp.TitleText = "Недостаточно данных";
                    wp.Message = message;
                    wp.ShowDialog();
                    e.Cancel = true;
                    return;
                }
                if (manufacturer.ManufacturerID == 0)
                    manufacturer.Insert();
                else
                    manufacturer.Update();
            }
            else
                manufacturer.Cancel();
        }
    }
}
